package com.hf.eagle.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hf.eagle.dao.AccessoryDao;
import com.hf.eagle.dao.AccessoryRelDao;
import com.hf.eagle.dao.SysDictionaryDao;
import com.hf.eagle.domain.Accessory;
import com.hf.eagle.domain.AccessoryRel;
import com.hf.eagle.domain.SysDictionary;
import com.hf.eagle.service.SysDictionaryService;

/**
 * @author Xu,XiaoYuan
 * @version 2011-09-06 23:44
 */
@Service
public class SysDictionaryServiceImpl implements SysDictionaryService {

	@Resource
	private SysDictionaryDao sysDictionaryDao;

	@Resource
	private AccessoryDao accessoryDao;

	@Resource
	private AccessoryRelDao accessoryRelDao;

	public Long createSysDictionary(SysDictionary t) {
		SysDictionary sd = new SysDictionary();
		sd.setDictionary_id(t.getDictionary_id());
		Integer dictionary_value = sysDictionaryDao.selectSysDictionaryMaxDictionaryValue(sd);
		if (null == dictionary_value) {
			dictionary_value = 1;
		} else {
			dictionary_value += 1;
		}

		t.setDictionary_value(dictionary_value);
		Long id = this.sysDictionaryDao.insertEntity(t);

		List<Accessory> accList = t.getAccessoryList();
		if (null != accList && accList.size() > 0) {
			for (Accessory accessory : accList) {
				Long acce_id = accessoryDao.insertEntity(accessory);

				AccessoryRel accessoryRel = new AccessoryRel();
				accessoryRel.setMod_id(0l);
				accessoryRel.setRecord_id(id);
				accessoryRel.setAcce_id(acce_id);

				accessoryRelDao.insertEntity(accessoryRel);
			}
		}
		return (long) dictionary_value;
	}

	public SysDictionary getSysDictionary(SysDictionary t) {
		return this.sysDictionaryDao.selectEntity(t);
	}

	public Long getSysDictionaryCount(SysDictionary t) {
		return this.sysDictionaryDao.selectEntityCount(t);
	}

	public List<SysDictionary> getSysDictionaryList(SysDictionary t) {
		return this.sysDictionaryDao.selectEntityList(t);
	}

	public int modifySysDictionary(SysDictionary t) {
		List<Accessory> accList = t.getAccessoryList();
		if (null != accList && accList.size() > 0) {
			/************ 删除原先的附件 ************/
			AccessoryRel acce_rel = new AccessoryRel();
			acce_rel.setMod_id(0l);
			acce_rel.setRecord_id(t.getId());
			List<AccessoryRel> list = accessoryRelDao.selectEntityList(acce_rel);

			String[] pks = null;
			if (null != list && list.size() > 0) {
				pks = new String[list.size()];
				for (int i = 0; i < list.size(); i++) {
					pks[i] = String.valueOf(list.get(i).getAcce_id());

					accessoryRelDao.deleteEntity(list.get(i));
				}
			}

			if (null != pks && pks.length > 0) {
				Accessory acce = new Accessory();
				acce.getMap().put("pks", pks);
				accessoryDao.deleteEntity(acce);
			}
			/************ 删除原先的附件 ************/

			for (Accessory accessory : accList) {
				Long acce_id = accessoryDao.insertEntity(accessory);

				AccessoryRel accessoryRel = new AccessoryRel();
				accessoryRel.setMod_id(0l);
				accessoryRel.setRecord_id(t.getId());
				accessoryRel.setAcce_id(acce_id);

				accessoryRelDao.insertEntity(accessoryRel);
			}
		} else if (null != t.getMap().get("acce_id") && !"".equals((String) t.getMap().get("acce_id"))) { // 修改原附件相关信息(标题,简介等)
			String mod_id = (String) t.getMap().get("mod_id");
			String title = (String) t.getMap().get("title");
			String description = (String) t.getMap().get("description");

			Accessory acce = new Accessory();
			acce.setTitle(title);
			acce.setDescription(description);
			acce.getMap().put("mod_id", mod_id);
			acce.getMap().put("record_id", t.getId());

			accessoryDao.updateEntity(acce);
		}

		return this.sysDictionaryDao.updateEntity(t);
	}

	public int removeSysDictionary(SysDictionary t) {
		return this.sysDictionaryDao.updateEntity(t);
	}

	public List<SysDictionary> getSysDictionaryPaginatedList(SysDictionary t) {
		return this.sysDictionaryDao.selectEntityPaginatedList(t);
	}

	public int getSysDictionaryMaxDictionaryValue(SysDictionary t) {
		return this.sysDictionaryDao.selectSysDictionaryMaxDictionaryValue(t);
	}

	public int removeSysDictionaryByValue(SysDictionary t) {
		return this.sysDictionaryDao.deleteSysDictionaryByValue(t);
	}

}
